﻿using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using SocialCee.EDM;
using System.Collections.Generic;
using System.Threading;

namespace SocialCee.DAL.Tests
{
    [TestClass]
    public class Posts
    {
        #region Insert
        [TestMethod]
        public void Insert()
        {
            using (SocialCeeDB_Entities context = new SocialCeeDB_Entities())
            {
                SocialNetworkUsers user = new SocialNetworkUsers();
                user.ID = Guid.NewGuid();
                user.Name = "Marcin";
                user.Surname = "Zyla";
                user.SocialNetworkTypeID = 1;//FB
                user.SocialNetworkUserID = Guid.NewGuid();
                context.SocialNetworkUsers.Add(user);

                SocialNetworkPost snp = new SocialNetworkPost();
                snp.Date = DateTime.UtcNow;
                snp.ID = Guid.NewGuid();
                snp.Post = "test post";
                snp.UserID = user.ID;
                context.SocialNetworkPost.Add(snp);

                Assert.IsTrue(context.SaveChanges() > 0);
            }
        }

        [TestMethod]
        public void Insert_WtihComments()
        {
            using (SocialCeeDB_Entities context = new SocialCeeDB_Entities())
            {
                SocialNetworkUsers user = new SocialNetworkUsers();
                user.ID = Guid.NewGuid();
                user.Name = "Marcin";
                user.Surname = "Zyla";
                user.SocialNetworkTypeID = 1;//FB
                user.SocialNetworkUserID = Guid.NewGuid();
                context.SocialNetworkUsers.Add(user);

                SocialNetworkPost snp = new SocialNetworkPost();
                snp.Date = DateTime.UtcNow;
                snp.ID = Guid.NewGuid();
                snp.Post = "test post";
                snp.UserID = user.ID;
                context.SocialNetworkPost.Add(snp);

                for (int a = 0; a < 10; a++)
                {
                    SocialNetworkPostComments snpc = new SocialNetworkPostComments();
                    snpc.ID = Guid.NewGuid();
                    snpc.Comment = string.Format("Test comment {0}", a);
                    snpc.CommentUserID = string.Format("Some user id {0}", a); ;
                    snpc.CommentUserName = "Jan";
                    snpc.CommentUserSurname = "Kowalski";
                    snpc.Date = DateTime.UtcNow;
                    snpc.PostID = snp.ID;
                    context.SocialNetworkPostComments.Add(snpc);
                    Thread.Sleep(10);
                }
                Assert.IsTrue(context.SaveChanges() > 0);
            }
        }
        #endregion

        [TestMethod]
        public void Get()
        {
            List<SocialNetworkPost> lp = SocialCee.DAL.Posts.GetPosts();

            Assert.IsNotNull(lp);
            Assert.IsTrue(lp.Count > 0);
        }

        [TestMethod]
        public void WithComments()
        {
            List<SocialNetworkPost> lp = SocialCee.DAL.Posts.GetPosts(true);

            Assert.IsNotNull(lp);
            Assert.IsTrue(lp.Count > 0);
            Assert.IsNotNull(lp[0].SocialNetworkPostComments);
        }

    }
}
